package com.housing.service.impl;

import com.zmetaport.common.core.utils.MapstructUtils;
import com.zmetaport.common.core.utils.StringUtils;
import com.zmetaport.common.mybatis.core.page.TableDataInfo;
import com.zmetaport.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.housing.domain.bo.AttachmentBo;
import com.housing.domain.vo.AttachmentVo;
import com.housing.domain.Attachment;
import com.housing.mapper.AttachmentMapper;
import com.housing.service.IAttachmentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author chensm
 * @date 2024-12-15
 */
@RequiredArgsConstructor
@Service
public class AttachmentServiceImpl implements IAttachmentService {

    private final AttachmentMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public AttachmentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<AttachmentVo> queryPageList(AttachmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Attachment> lqw = buildQueryWrapper(bo);
        Page<AttachmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<AttachmentVo> queryList(AttachmentBo bo) {
        LambdaQueryWrapper<Attachment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Attachment> buildQueryWrapper(AttachmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Attachment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSourceType() != null, Attachment::getSourceType, bo.getSourceType());
//        lqw.eq(bo.getSourceId() != null, Attachment::getSourceId, bo.getSourceId());
        lqw.eq(bo.getFileType() != null, Attachment::getFileType, bo.getFileType());
        lqw.eq(StringUtils.isNotBlank(bo.getFileUrl()), Attachment::getFileUrl, bo.getFileUrl());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(AttachmentBo bo) {
        Attachment add = MapstructUtils.convert(bo, Attachment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(AttachmentBo bo) {
        Attachment update = MapstructUtils.convert(bo, Attachment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Attachment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
